#ifndef SAVEINMAP_H_
#define SAVEINMAP_H_
#include <stdio.h>
#include <map>
#include <vector>
#include <stdexcept>
using namespace std;

struct State
{
  int flow;
  int cost;
  State() {}
  State(int f, int c)
  {
    flow = f;
    cost = c;
  }
};
class SaveInMap
{
private:
  int n;
  map<string, State> state_map;

public:
  SaveInMap() {}
  SaveInMap(int n)
  {
    SaveInMap::n = n;
  }
  bool InsertStateToMap(vector<int> state, State now_state)
  {
    if (n != (int)state.size())
    {
      throw runtime_error("n != state.size()\n");
      return false;
    }
    string sstate;
    for (int i = 0; i < n; i++)
    {
      if (state[i] < -1 || state[i] > 1)
      {
				throw runtime_error("state[i] < -1 || state[i]>1\n");
      }
      int temp = '1' + state[i];
      sstate.push_back(temp);
    }
    state_map.insert(pair<string, State>(sstate, now_state));
    // PRINTF("insert state:(f:%d,c:%d)\tmap_size:%lld\n",
	  //  state_map[sstate].flow, state_map[sstate].cost,(long long int)state_map.size());
    return true;
  }
  State FindStateInMap(vector<int> state)
  {
    State ret_false(-1, -1);
    if (n != (int)state.size())
    {
      throw runtime_error("n != state.size()\n");
      return ret_false;
    }
    string sstate;
    for (int i = 0; i < n; i++)
    {
      if (state[i] < -1 || state[i] > 1)
      {
				throw runtime_error("state[i] < -1 || state[i]>1\n");
      }
      int temp = '1' + state[i];
      sstate.push_back(temp);
    }
    map<string, State>::iterator it = state_map.find(sstate);
    if (it == state_map.end())
    {
      // PRINTF("no value for key:%s\n", sstate.c_str());
      return ret_false;
    }
    // PRINTF("get key:%s\tstate:(f:%d,c:%d)\n",
	  //  sstate.c_str(), state_map[sstate].flow, state_map[sstate].cost);
    return it->second;
  }
};

#endif
